package cn.bopomo.dealer.base.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import cn.bopomo.dealer.base.common.ServiceConfig;
import cn.bopomo.dealer.base.service.IOperatorService;
import cn.bopomo.dealer.base.util.ResultCode;
import cn.bopomo.dealer.base.util.SecurityUtil;
import cn.bopomo.dealer.base.vo.OperatorDetail;

public class ModifyOperatorPwdAction extends BaseAction {

	
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		log.info("========>>接收请求  [修改操作员登录密码] IP:[ "+ request.getRemoteAddr() +  " ]");
		//获取当前操作员ID
		String userId = this.getUserId(request);
		if(userId == null){
			return this.findFailure(mapping);
		}
		log.info("========>>当前用户ID:[ "+ userId +  " ]");
		//获取参数
		String oldPwd = request.getParameter("oldPwd");
		String newPwd = request.getParameter("newPwd");
		String confirmPwd = request.getParameter("confirmPwd");
		//检查密码一致性
		if(!newPwd.equals(confirmPwd)){
			return this.findFailure(mapping);
		}
		
		IOperatorService os = (IOperatorService)this.findService(ServiceConfig.OPERATOR_SERVICE);
		OperatorDetail operator = os.getOperatorDetail(userId);
		if(operator == null){
			return this.findFailure(mapping);
		}
		oldPwd = SecurityUtil.MD5(oldPwd);
		//检查原来密码
		if(!operator.getPassword().equals(oldPwd)){
			return this.findFailure(mapping);
		}
		operator.setPassword(SecurityUtil.MD5(newPwd));
		String result = os.modifyOperator(operator);
		
		if (!ResultCode.OP_SUCCESS.equals(result)){
			//报错
			return this.findFailure(mapping);
		}
		request.setAttribute("info", "修改密码成功");
		return this.findSuccess(mapping);
	}
}
